<template>
<div id="container">
    <div id="header">
        <div class="back_home" @click="backIndex">课堂</div>
        <div class="text">></div>
        <div class="text">{{course.courseName}}</div>
        <div class="text">{{course.className}}</div>
    </div>
    <div id="body">
        <div class="title">
            <div class="course_name">{{course.courseName}}</div>
            <div class="class_name">{{course.className}}</div>
            <div class="code">{{course.courseCode}}</div>
        </div>
        <div class="nav">
            <el-menu mode="horizontal" :default-active="currentPage" @select="changePage">
                <el-menu-item index="1">课堂互动</el-menu-item>
                <el-menu-item index="2">作业</el-menu-item>
                <el-menu-item index="3">话题</el-menu-item>
                <el-menu-item index="4">资料</el-menu-item>
                <el-menu-item index="5">测试（考试）</el-menu-item>
                <el-menu-item index="6">公告</el-menu-item>
            </el-menu>
        </div>

        <div class="pageShow" v-if="currentPage==='2'">
            <div class="work_container">
                <div class="work_control" v-if="isOwner">
                    <div class="work_control_add">发布个人作业</div>

                    <div class="work_manage"><i class="el-icon-s-order"></i>作业成绩管理</div>
                    <div class="work_download_all"><i class="el-icon-download"></i>下载所有作业</div>
                </div>
                <div v-for="item in work" v-bind:key="item.id" class="work_item">
                    <div class="work_title">
                        个人作业
                    </div>
                    <a class="work_name" @click="submit(item.id)">
                        {{item.name}}
                    </a>
                    <div class="work_describe">
                        {{item.describe}}
                    </div>
                    <br>
                    <span class="work_deadline">
                        <span style="margin-right: 10px">截至时间</span>
                        <span>{{item.deadLine}}</span>
                    </span>
                    <div class="work_commit" @click="submit(item.id)">
                        提交
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</template>

<script>
import axios from 'axios'
export default {
    name: "Course",
    data() {
        return {
            course: null,
            currentPage: "1",
            work: null,
            user: null,
            isOwner: false
        }
    },
    methods: {
        getQueryVariable: function (variable) {
            const query = window.location.search.substring(1);
            const vars = query.split("&");
            for (let i = 0; i < vars.length; i++) {
                const pair = vars[i].split("=");
                if (pair[0] === variable) { return pair[1]; }
            }
            return false;
        },
        getCourse: function () {
            let id = this.getQueryVariable("id")
            axios.get("http://localhost:8080/course/get_course_by_id", {
                    params: {
                        "id": id
                    }
                })
                .then((res) => {
                    console.log(res.data);
                    localStorage.setItem("id", id);
                    this.course = res.data
                })
        },
        changePage: function (key) {
            this.currentPage = key
            if (key === "2") {
                axios.get("http://localhost:8080/work/sel_work_by_course_id", {
                        params: {
                            "courseId": this.course.id
                        }
                    })
                    .then((res) => {
                        console.log(res.data)
                        this.work = res.data
                    })
                if (this.user.id === this.course.creatorId) {
                    this.isOwner = true
                    console.log("is owner")
                } else {
                    this.isOwner = false
                    console.log("not owner")
                }
            }
        },
        backIndex: function () {
            window.location.href = "./index"
        },
        checkCourseOwner: function () {
            let username = sessionStorage.getItem("username")
            axios.get("http://localhost:8080/user/get_user_by_mail_or_tel", {
                    params: {
                        "value": username
                    }
                })
                .then((res) => {
                    this.user = res.data[0]
                })
        },
        submit: function (index) {
            window.location.href = '/submit?workId=' + index;
        }
    },

    created() {
        this.getCourse()
        this.checkCourseOwner()
        // 设置休眠时间
        function sleep(time) {
            return new Promise((resolve) => setTimeout(resolve, time));
        }
        // 等待半秒后获取数据
        sleep(500).then(() => {

        })
    }
}
</script>

<style lang="scss" scoped>
#container {
    width: 100%;
    display: flex;
    flex-direction: column;
}

#header {
    margin-top: 10px;
    margin-left: 100px;

    height: 65px;

    display: flex;
    flex-direction: row;

    div {
        height: 65px;
        line-height: 65px;
    }

    .back_home {
        font-size: 14px;
        font-weight: 400;
        color: #5F6368;
        margin-right: 10px;

        &:hover {
            cursor: pointer;
        }
    }

    .text {
        font-size: 14px;
        font-weight: 400;
        color: #5F6368;
        margin-right: 10px;
    }
}

#body {
    display: flex;
    flex-direction: column;
    width: 1225px;
    margin: 50px auto;

    div {
        width: 1225px;
    }

    .title {
        height: 200px;
        background: url("../assets/courseImg/10.png");
        border-radius: 8px;

        display: flex;
        flex-direction: column;

        div {
            padding-left: 40px;
        }

        .course_name {
            padding-top: 48px;
            padding-bottom: 5px;

            font-size: 36px;
            color: #fff;
            font-weight: 500;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
        }

        .class_name {
            font-size: 20px;
            font-weight: 500;
            color: rgba(255, 255, 255, 1);
            line-height: 28px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            margin-bottom: 20px;
        }

        .code {
            font-size: 14px;
            color: white;
        }
    }

    .nav {
        height: 72px;

        .el-menu {
            background-color: #F1F3F4 !important;
            padding-left: 35px;
            height: 72px !important;
            line-height: 72px !important;
        }

        .el-menu-item {
            margin-right: 20px;
            height: 72px !important;
            line-height: 72px !important;
            font-weight: 500;
            color: rgba(59, 61, 69, 1);

            &:focus {
                background-color: #F1F3F4 !important;
            }

            &:hover {
                color: blue;
                background-color: #F1F3F4 !important;
            }
        }

    }

    .pageShow {
        margin-top: 25px;
        padding-bottom: 150px;
        background-color: #fff;
        border-radius: 4px;
        /*border: 1px solid rgba(226,230,237,1);*/

        .work_container {
            display: flex;
            flex-direction: column;

            .work_control {
                display: flex;
                flex-direction: row;

                height: 50px;

                .work_control_add {
                    height: 36px;
                    width: 136px;
                    border-radius: 4px;
                    border: 1px solid #4d90fe;
                    color: #4d90fe;
                    font-size: 14px;
                    text-align: center;
                    line-height: 36px;

                    &:hover {
                        cursor: pointer;
                    }
                }

                .work_download_all {
                    height: 36px;
                    line-height: 36px;

                    font-size: 14px;
                    width: 200px;

                    text-align: right;

                    &:hover {
                        cursor: pointer;
                    }
                }

                .work_manage {
                    height: 36px;
                    line-height: 36px;

                    font-size: 14px;
                    width: 200px;
                    flex-grow: 1;

                    text-align: right;

                    &:hover {
                        cursor: pointer;
                    }
                }

            }

            .work_item {
                color: #5F6368;
                border: 1px solid #E2E6ED;

                width: 1165px;
                margin-bottom: 20px;
                border-radius: 8px;

                padding-left: 40px;
                padding-right: 20px;

                height: 208px;

                .work_title {
                    margin-top: 20px;
                    font-size: 14px;
                    height: 30px;
                    line-height: 30px;
                }

                .work_name {
                    height: 38px;
                    width: auto;
                    line-height: 38px;

                    font-size: 20px;
                    font-weight: 400;

                    &:hover {
                        cursor: pointer;
                        color: #4d90fe;
                        border-bottom: 2px solid #4d90fe;
                    }
                }

                .work_describe {
                    height: 40px;
                    width: 1100px;
                    font-size: 14px;
                    overflow: auto;
                    color: #2d2d2d;
                }

                .work_deadline {
                    color: #A0A0A0;

                    height: 20px;
                    line-height: 20px;
                    font-size: 14px;

                    margin-top: 20px;
                }

                .work_commit {
                    width: 80px !important;
                    height: 30px !important;
                    line-height: 30px !important;
                    color: #32BAF0 !important;
                    border: 1px solid #32BAF0 !important;
                    float: right;

                    border-radius: 4px;
                    font-size: 14px;

                    text-decoration: none;
                    outline: none;
                    text-align: center;

                    margin-right: 40px;
                    margin-bottom: 40px;

                    &:hover {
                        cursor: pointer;
                    }
                }
            }
        }
    }
}
</style>
